/*
Content: Tables 8, 9
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/
	
	
*****************************
** MECHANISMS
*****************************

 use "$data/finaldata_terrorsoep_light_final.dta", clear

* Create cluster variable 

egen cluster3 = group(soep_corigin_id syear month)
cap gen distanceterror_m3y_p90_10 = distanceterror_m3y_p90 + 10


********************************************************************************	
**# Table 8: Job search activity among unemployed immigrants
********************************************************************************	

	 eststo clear 
local i=1	
	foreach z in  findfuturejob urgency pref_hours ln_reswage_defl ln_h_reswage_defl  {

foreach var in m3y  {
	
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_`var'_p90,-3,3) & !inlist(distanceterror_`var'_p90,0) & mratiointerterror_m3y_p90!=1"
global controls "i.soep_corigin_id#i.syear i.month#i.syear i.bula" 	

qui reghdfe remain_ger_per postterror_`var'_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M, vce(cluster cluster3) absorb($controls)
cap drop  sample_terror_`var'
qui:  g sample_terror_`var'=e(sample)
			
qui  eststo n`i': reghdfe `z' postterror_m3y_p90 i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_terror_`var' ==1  , vce(cluster cluster3) absorb($controls)
 estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
sum	 remain_ger_per if e(sample)==1 & postterror_`var'_p90==0
estadd scalar ymean = r(mean)
local i=`i'+1
}
}

* JOB SEARCH IN LAST 4 weeks 
// Note: need to run separately because we want to exclude the first month after the terror month (due to the framing of the question) 

foreach var in m3y  {

global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_`var'_p90,-3,3) & !inlist(distanceterror_`var'_p90,0,1)"

global controls "i.soep_corigin_id#i.syear i.bula i.month#i.syear" 	

qui eststo jobsearch: reghdfe jobsearch postterror_`var'_p90 i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_terror_`var' == 1, vce(cluster cluster3) absorb($controls)
estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
sum	 remain_ger_per if e(sample)==1 & postterror_`var'_p90==0
estadd scalar ymean = r(mean)

		}
	


esttab jobsearch n* using "$table/otheroutcomes_supporting_final.tex", star(* 0.10 ** 0.05 *** 0.01) replace b(3) se(3) drop(*sex age *ysm *maritstat *educ_aftm2 *child _cons) nocons  nogaps nonotes noobs  mgroups("Higher than average of last 3 years", ///
            pattern(1 0 0 0 0 0) ///
            prefix(\multicolumn{@span}{c}{) suffix(}) ///
            span erepeat(\cmidrule(lr){@span})) varlabel(postterror_m3y_p90 "Post-Terror")  mtitles("\shortstack{Active in\\last 4 weeks}"  "\shortstack{Plan to\\find job}" "\shortstack{Find job\\asap}" "\shortstack{Preferred\\nr hours}" "\shortstack{Ln monthly\\res. wage}" "\shortstack{Ln hourly\\res. wage}" ) scalars( "nobs Observations" "ymean Mean intention to stay"  "orig_y Origin country x Year FE" "date Month FE x Year FE" "orig_m Origin country x Month FE" "state State of Residency FE" "indiv Indiv. Controls") sfmt(%9.0fc %9.3f %1s %1s %1s %1s %1s)


			    
  


********************************************************************************	
**# Table 9: Other mediating channels
********************************************************************************				
			

	 eststo clear 
local i=1	
	foreach z in  send_money_abroad health_satisfaction  {

foreach var in m3y  {
	
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_`var'_p90,-3,3) & !inlist(distanceterror_`var'_p90,0) & mratiointerterror_m3y_p90!=1"
global controls "i.soep_corigin_id#i.syear i.month#i.syear i.bula" 	

qui reghdfe remain_ger_per postterror_`var'_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M, vce(cluster cluster3) absorb($controls)
cap drop  sample_terror_`var'
qui:  g sample_terror_`var'=e(sample)
			
qui  eststo n`i': reghdfe `z' postterror_m3y_p90 i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_terror_`var' ==1  , vce(cluster cluster3) absorb($controls)
 estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)
local i=`i'+1
}
}

esttab n* using "$table/otheroutcomes_additional_final.tex", star(* 0.10 ** 0.05 *** 0.01) replace b(3) se(3) drop(*sex age *ysm *maritstat *educ_aftm2 *child _cons) nocons  nogaps nonotes noobs  mgroups("Higher than average of last 3 years", ///
            pattern(1 0 0 0 0 0) ///
            prefix(\multicolumn{@span}{c}{) suffix(}) ///
            span erepeat(\cmidrule(lr){@span})) varlabel(postterror_m3y_p90 "Post-Terror")  mtitles("\shortstack{Send money\\abroad}"  "\shortstack{Self-rated health\\satisfaction}"  ) scalars( "nobs Observations" "ymean Mean intention to stay"  "orig_y Origin country x Year FE" "date Month FE x Year FE" "orig_m Origin country x Month FE" "state State of Residency FE" "indiv Indiv. Controls") sfmt(%9.0fc %9.3f %1s %1s %1s %1s %1s)


			 

